package com.qida.clm.core.utils;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * Created by xiexiang on 2015/10/30.
 */
public class StorageUtils {

    private static final String SD_ROOT_PATH = Environment.getExternalStorageDirectory().getPath();
    public static long getAvailableSize(){
        StatFs statFs = new StatFs(SD_ROOT_PATH);
        long available = 0;
        long totalSize = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            available = statFs.getAvailableBlocksLong();
            totalSize = statFs.getBlockSizeLong();
        } else {
            available = statFs.getAvailableBlocks();
            totalSize = statFs.getBlockSize();
        }
        return available * totalSize;
    }

    public static boolean isExternalStorageRemovable(){
        return Environment.isExternalStorageRemovable();
    }

    public static boolean isAvailable(long size){
        return getAvailableSize() > size;
    }

    /**
     * Check how much usable space is available at a given path.
     *
     * @param path The path to check
     * @return The space available in bytes
     */
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static long getUsableSpace(File path) {
        if (VersionUtils.hasGingerbread()) {
            return path.getUsableSpace();
        }
        final StatFs stats = new StatFs(path.getPath());
        return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
    }
}
